package com.yykj.common.aspect;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.service.RedisService;
import com.yykj.common.util.LocalRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * 系统操作登录校验拦截
 * Created by chendg
 * on 2018/1/9 0009.
 */
@Order(1)
@Aspect
@Slf4j
@Component
public class TokenAuthorizeAspect {

    @Pointcut("(execution(public * com.yykj..backstage..controller..*(..)) || " +
            "execution(public * com.yykj..drug..controller..*(..)) || " +
            "execution(public * com.yykj..user..controller..*(..)) || " +
            "execution(public * com.yykj..hospital..controller..*(..)) ) && " +
            "!execution(public * com.yykj..controller..login(..)) && " +
            "!execution(public * com.yykj..controller..loginInfo(..)) && " +
            "!execution(public * com.yykj..controller..userSign(..)) && " +
            "!execution(public * com.yykj..controller..createDefault(..)) && " +
            "!execution(public * com.yykj..controller..createAccount(..)) && " +
            "!execution(public * com.yykj..controller..codeLogin(..)) && " +
            "!execution(public * com.yykj..controller..notify(..)) && " +
            "!execution(public * com.yykj..controller..isRegister(..)) && " +
            "!execution(public * com.yykj..controller..sendCode(..)) && " +
            "!execution(public * com.yykj..controller..register(..)) && " +
            "!execution(public * com.yykj..controller..downloadFile(..)) && " +
            "!execution(public * com.yykj..controller..web..*(..)) && " +
            "!execution(public * com.yykj..controller..export*(..)) && " +
            "!execution(public * com.yykj..controller..getMobileCheckCode(..)) && " +
            "!execution(public * com.yykj..controller..resetPassword(..)) && " +
            "!execution(public * com.yykj..controller..viewHospitalAgreement(..)) && " +
            "!execution(public * com.yykj..controller.PatientClientHomeController..*(..)) && " +
            "!execution(public * com.yykj..controller.Proto*.*(..))"
    )
    public void verify() {
    }

    /**
     * 检查token
     *
     * @param joinPoint 切入点
     */
    @Before("verify()")
    public void doVerify(JoinPoint joinPoint) throws IOException {
        String token = LocalRequestUtil.getToken();
        log.info(" token = {}", token);
        if (StringUtils.isEmpty(token)) {
            throw new NormalException(ResultEnums.HEAD_TOKEN_NOT_EXIST_ERROR);
        }
        String tokenValue = redisService.get(token);
        if (StringUtils.isEmpty(tokenValue)) {
            throw new NormalException(ResultEnums.HEAD_TOKEN_ERROR);
        }
        redisService.reset(token);//刷新，看情况*/
    }


    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

}
